/**
 * 资源树表格
 */
Ext.define('BenBen.modules.system.menu.MenuTreeGrid', {
    extend: 'BenBen.common.BaseTreeGrid',
    initComponent: function () {
    	var me = this;
    	
    	Ext.define('WXMenu', {
            extend: 'Ext.data.Model',
            fields: [
                {name: 'id',     type: 'string'},
                {name: 'name',     type: 'string'},
                {name: 'agentId',     type: 'string'},
                {name: 'type',     type: 'string'},
                {name: 'url', type: 'string'},
                {name: 'state', type: 'string'},
                {name: 'key',     type: 'string'}
            ]
        });

    	var store = Ext.create('Ext.data.TreeStore', {
            model: 'WXMenu',
            proxy: {
                type: 'ajax',
                url: 'system/menu'
            }
        });
    	
    	
        
        var form = Ext.create('BenBen.modules.system.menu.MenuForm');
        
        var columns = [{
        	dataIndex: 'name',
        	xtype: 'treecolumn',
        	text: '菜单名'
        },{
            dataIndex: 'agentId',
            text: '应用编号'
        },{
        	dataIndex: 'type',
        	text: '类型'
        },{
        	dataIndex: 'key',
        	text: 'KEY'
        },{
        	dataIndex: 'url',
        	text: 'URL'
        },{
            dataIndex: 'state',
            text: '状态',
            renderer: function (value) {
                if (value == 1) {
                    return "有效";
                }else if (value == 0) {
                    return "停用";
                }
            }
        }];
        
        var tbarItems = [{
        	xtype : 'combo',
        	name: 'agentId',
            fieldLabel: '应用编号',
            mode : 'local',
            width : 150,
            editable : false,
            labelAlign : 'right',
            value : '',
            triggerAction : 'all',
            displayField : 'name',
            valueField : 'name',
            store : new Ext.data.SimpleStore({
                fields : ['name'],
                data : [[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]]
            }),
            listeners : {
    			select : function(combo, record, index) {
    				me.agentId = combo.getValue(); 
    				me.store.load({
    					params : {
    						agentId : me.agentId
    					}
    				});
    			}
    		}
        },{
            text : '同步到微信',
            iconCls : 'gridupdate',
            handler : function(){
                if(me.agentId){
                	var url = 'system/menu/wx/'+me.agentId;
                	Ext.Ajax.request({
                        url: url,
                        method: 'PUT',
                        callback: function (options, success, response) {
                            if (success) {
                                if (Ext.decode(response.responseText).success) {
                                	Ext.Msg.confirm('成功','同步数据成功');
        						} else {
        							Ext.Msg.confirm('失败',Ext.decode(response.responseText).info);
        						}
                                me.reload();
                            } else {
                                Ext.Msg.confirm('失败','同步数据失败');
                            }
                        }
                    });
                }
            	
            }
        }];
        
    	Ext.apply(this,{
            border : false,
            store: store,
            tbarDefaultItems : "add,update,delete,look",
            dataForm : form,
			columns : columns,
			tbarItems : tbarItems,
			config : {
				winWidth : 400,
                winHeigth : 210,
				pageSize : 30,
				url : 'system/menu'
			},
			height: 540
        });
    


        this.callParent(arguments);
    }
});
